package com.ylx.web.u;

import com.ylx.pojo.User;
import com.ylx.service.UserService;
import com.ylx.service.UserServiceIpml;
import com.ylx.util.UploadFileUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;

@WebServlet("/updateTouXiangServlet")
public class UpdateTouXiangServlet extends HttpServlet {
    protected void doPost (HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        UserService us = new UserServiceIpml();

        //要修改头像
        String zp = UploadFileUtil.uploadFile(request,response);
        //上传成功后需要将照片的名字保存到数据库中
        //获取登录的用户账号
        HttpSession session = request.getSession();
        User user = (User) session.getAttribute("us");
         user.setZp(zp);
        System.out.println("--->"+user);
        int i=us.updateZp(user);
        if (i>0) {
            System.out.println("头像修改成功");
            //将修改后的xsb重写放到session中
            session.setAttribute("us", user);
            //拼接前端的json格式
            String jsonrs = "{\"code\":0,\"msg\":\"\",\"data\":\"\"}";
            System.out.println(jsonrs);
            //输出在浏览器
            PrintWriter out = response.getWriter();
            out.println(jsonrs);
//
//            String zp = UploadFileUtil.uploadFile(request, response);
//            HttpSession session = request.getSession();
//            xsb xsb = (xsb) session.getAttribute("xs");
//            xsb.setZp(zp);
//
//            System.out.println("--->"+xsb);
//            int i= xs.updateZp(xsb);
//            if(i>0){
//                System.out.println("头像修改成功");
//                session.setAttribute("xs",xsb);
//                String jsonrs ="{\"code\":0,\"msg\":\"\",\"data\":\"\"}";
//
//                //输出到浏览器
//                PrintWriter out = response.getWriter();
//                out.println(jsonrs);
        }
    }

    protected void doGet (HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        this.doPost(request, response);

    }
}
